import tableHtml from 'ngtemplate!html!../../../tpls/score/table.html';

class ScoreController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, ScoreService, tableService, getCourseList, iRunner){

        var vm = this;

        vm.$rootScope    = $rootScope;
        vm.$cookieStore  = $cookieStore;
        vm.$timeout      = $timeout;
        vm.$state        = $state;
        vm.ngDialog      = ngDialog;
        vm.dialogService = dialogService;
        vm.ScoreService  = ScoreService;
        vm.tableService  = tableService;
        vm.getCourseList = getCourseList;
        vm.iRunner       = iRunner;

        // 科目
        vm.course = '';

        // 模版
        vm.template = tableHtml;

        // 科目列表
        if (vm.getCourseList.code==0) {
            vm.courselist = vm.getCourseList.data;

            if (vm.courselist.length>0) {
                vm.course = vm.courselist[0];
                vm.$timeout(() => {
                    getScoreData(vm.course.course_id);
                }, 0)
            }
        }

        // 监听科目变化
        vm.onchange = (course) => {
            if (course) {

                vm.$timeout(() => {
                    vm.template = '';
                }, 0)
                vm.$timeout(() => {
                    vm.template = tableHtml;
                }, 0)
                vm.$timeout(() => {
                    getScoreData(course.course_id);
                }, 0)
                
                console.log(course)
            } else if (course == undefined) {

                vm.$timeout(() => {
                    vm.template = '';
                }, 0)
                vm.$timeout(() => {
                    vm.template = tableHtml;
                }, 0)
                vm.$timeout(() => {
                    getScoreData();
                }, 0)

            }
        }

        vm.clear = function() {

                vm.ScoreService.postScoreClear(vm.$cookieStore.get('access_token'), this.$state.params.race_id)
                .then( data => {
                    // 成功调用
                    data.code==0&&vm.dialogService.success('调用成功');
                    // 调用失败, 提示用户
                    data.code!=0&&vm.dialogService.error(data.message);
                    console.log(data)
                }, error => console.log('error'));     


        }

        // 初始化表格控件, ajax请求数据
        function getScoreData(course_id) {
            var course_id = course_id || 0;
            vm.tableService.getScoreData(null, vm.$cookieStore.get('access_token'), vm.$state.params.race_id + '&course_id=' + course_id);
        }

    }
}

ScoreController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'ScoreService', 'tableService', 'getCourseList', 'iRunner']; 

export default ScoreController;